use config::{ConfigError, Environment, File};
use serde::Deserialize;
use binance_sdk::config::Config;

#[derive(Clone, Debug, Deserialize)]
pub struct ZmqConfig {
    pub quote_pub: String,
    pub quote_sub: String,
}

// Redis Configuration
#[derive(Debug, Deserialize, Clone)]
pub struct RedisConfig {
    pub host: String,
    pub port: u16,
    pub password: String,
    pub db: u8,
    pub timeout_ms: u64,
}

#[derive(Clone, Debug, Deserialize)]
pub struct AppConfig {

    pub sdk: Config,

    pub zmq: ZmqConfig,

    pub redis: RedisConfig,

}

impl AppConfig {

    pub fn load(path: Option<&str>) -> Result<Self, ConfigError> {
        dotenv::dotenv().ok();

        let file_path = path.unwrap_or("config/config");

        let raw = config::Config::builder()
            .add_source(File::with_name(file_path).required(false))
            .add_source(Environment::default())
            .build()?;

        raw.try_deserialize()
    }

}
